package com.taptech.doufu.net.retrofit.api;

import android.app.Activity;

import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;

import java.io.File;
import java.io.IOException;

import okhttp3.ResponseBody;
import okio.BufferedSink;
import okio.Okio;
import retrofit2.Response;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * author zhangyongcun
 * 16/12/9
 */

public class RxJavaHelper {

    /**
     * 线程控制
     *
     * @param <T>
     * @return
     */
    public static <T> Observable.Transformer<T, T> observeOnMainThread() {
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> tObservable) {
                return tObservable
                        .subscribeOn(Schedulers.io()) //in io thread request
                        .observeOn(AndroidSchedulers.mainThread()); // main thread deal data
            }
        };
    }

    public static <T> Observable.Transformer<T, T> observeOnMainThread(Activity activity) {
        if (activity instanceof DiaobaoBaseActivity) {
            final DiaobaoBaseActivity baseActivity = (DiaobaoBaseActivity) activity;
            return new Observable.Transformer<T, T>() {
                @Override
                public Observable<T> call(Observable<T> tObservable) {
                    return tObservable
                            .subscribeOn(Schedulers.io()) //in io thread request
                            .observeOn(AndroidSchedulers.mainThread())
                            .compose(baseActivity.<T>bindToLifecycle()); // main thread deal data
                }
            };
        }else{
            return observeOnMainThread();
        }
    }


    /**
     * 下载文件
     * @param url
     * @param baseSubscriber
     */
    public static void downloadFile(String url, BaseSubscriber<File> baseSubscriber,final File dir, final String fileName) {
        ApiClient.getInstance().getService()
                .getUpdateApkFile(url)
                .flatMap(new Func1<Response<ResponseBody>, Observable<File>>() {
                    @Override
                    public Observable<File> call(final Response<ResponseBody> responseBodyResponse) {
                        return Observable.create(new Observable.OnSubscribe<File>() {
                            @Override
                            public void call(Subscriber<? super File> subscriber) {
                                try {
                                    File file = new File(dir, fileName);
                                    BufferedSink sink = Okio.buffer(Okio.sink(file));
                                    sink.writeAll(responseBodyResponse.body().source());
                                    sink.close();
                                    subscriber.onNext(file);
                                    subscriber.onCompleted();
                                } catch (IOException e) {
                                    e.printStackTrace();
                                    subscriber.onError(e);
                                }
                            }
                        });
                    }
                })
                .compose(RxJavaHelper.<File>observeOnMainThread())
                .subscribe(baseSubscriber);
    }
}
